package com.sgb.kjwl.view.ui.activity.home;

import androidx.databinding.DataBindingUtil;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityChoiceCityBinding;
import com.sgb.kjwl.di.home.DaggerHomeComponent;
import com.sgb.kjwl.di.home.HomeModule;
import com.sgb.kjwl.model.entity.home.CityEntity;
import com.sgb.kjwl.view.event.ChooseAreaEvent;
import com.swgk.core.event.EventConstant;
import com.sgb.kjwl.view.ui.adapter.home.CityListAdapter;
import com.sgb.kjwl.view.ui.adapter.home.ResultCityListAdapter;
import com.sgb.kjwl.view.widget.SideLetterBar;
import com.sgb.kjwl.viewmodel.home.ChoiceCityModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.util.PinyinUtils;
import org.greenrobot.eventbus.EventBus;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import javax.inject.Inject;

public class ChoiceCityActivity extends AppActivity {
    private ActivityChoiceCityBinding binding;
    @Inject
    ChoiceCityModel viewModel;
    private CityListAdapter mCityAdapter;
    private ResultCityListAdapter resultCityListAdapter;
    private ArrayList<CityEntity> cities;

    public static void start(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, ChoiceCityActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        context.startActivity(intent);
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerHomeComponent.builder()
                .appComponent(appComponent)
                .homeModule(new HomeModule(this))
                .build()
                .inject(this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(ChoiceCityActivity.this, R.layout.activity_choice_city);
        binding.include.ivBack.setOnClickListener(this);
        binding.include.ivCancel.setOnClickListener(this);
        binding.sideLetterBar.setOverlay(binding.tvLetterOverlay);
        binding.sideLetterBar.setOnLetterChangedListener(new SideLetterBar.OnLetterChangedListener() {
            @Override
            public void onLetterChanged(String letter) {
                int position = mCityAdapter.getLetterPosition(letter);
                binding.listviewAllCity.setSelection(position);
            }
        });


        binding.include.editSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (TextUtils.isEmpty(s)){
                    binding.rlSearchCityView.setVisibility(View.GONE);
                    binding.include.ivCancel.setVisibility(View.GONE);
                }else {
                    binding.include.ivCancel.setVisibility(View.VISIBLE);
                    binding.rlSearchCityView.setVisibility(View.VISIBLE);
                    viewModel.searchFilterModel(cities, s.toString());
                }
            }
        });

        binding.tvCurrentCity.setText(TextUtils.isEmpty(viewModel.getCityName())?"":viewModel.getCityName());
        //所有城市列表
        mCityAdapter = new CityListAdapter(this);
        binding.listviewAllCity.setAdapter(mCityAdapter);
        //搜索城市列表
        resultCityListAdapter = new ResultCityListAdapter(this);
        binding.lvSearchCity.setAdapter(resultCityListAdapter);
    }

    @Override
    protected void init() {
        viewModel.getCityList();
        mCityAdapter.setOnCityClickListener(new CityListAdapter.OnCityClickListener() {
            @Override
            public void onCityClick(CityEntity cityEntity) {//选择城市
                viewModel.setCity(cityEntity);
                binding.tvCurrentCity.setText(viewModel.getCityName());
                //todo 定位存入mmkv,便于全局调用，发送原切换定位的广播
                EventBus.getDefault().postSticky(ChooseAreaEvent.make(EventConstant.EVENT_CHOOSE_AERA_Code, cityEntity));
                finish();
            }
        });

        resultCityListAdapter.setOnCityClickListener(new ResultCityListAdapter.OnCityClickListener() {
            @Override
            public void onCityClick(CityEntity cityEntity) {
                viewModel.setCity(cityEntity);
                binding.tvCurrentCity.setText(viewModel.getCityName());
                //todo 定位存入mmkv,便于全局调用，发送原切换定位的广播
                EventBus.getDefault().postSticky(ChooseAreaEvent.make(EventConstant.EVENT_CHOOSE_AERA_Code, cityEntity));
                finish();
            }
        });
    }

    /**
     * 全部城市列表
     * @param cityDatas
     */
    public void setCityDatas(List<CityEntity> cityDatas){
        HashSet<CityEntity> citys = new HashSet<>();
        for (CityEntity cityEntity : cityDatas) {
            citys.add(new CityEntity(cityEntity.getCityCode(), cityEntity.getCityName(), PinyinUtils.getPingYin(cityEntity.getCityName())));
        }

        //set转换list
        cities = new ArrayList<>(citys);

        //按照字母排序
        Collections.sort(cities, new Comparator<CityEntity>() {
            @Override
            public int compare(CityEntity city, CityEntity t1) {
                return city.getPinyin().compareTo(t1.getPinyin());
            }
        });

        mCityAdapter.setData(cities);
    }

    /**
     * 搜索结果列表
     * @param cityDatas
     */
    public void setSearchCitys(List<CityEntity> cityDatas){
        if (cityDatas.size() == 0){
            //空布局
            binding.rlSearchCityView.setVisibility(View.VISIBLE);
            binding.lvSearchCity.setVisibility(View.GONE);
            binding.llEmptyView.setVisibility(View.VISIBLE);
        }else {
            binding.rlSearchCityView.setVisibility(View.VISIBLE);
            binding.lvSearchCity.setVisibility(View.VISIBLE);
            binding.llEmptyView.setVisibility(View.GONE);
            resultCityListAdapter.changeData(cityDatas);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_back:
                finish();
                break;
            case R.id.iv_cancel:
                binding.include.editSearch.setText("");
                binding.include.editSearch.setFocusable(false);
                binding.rlSearchCityView.setVisibility(View.GONE);
                closeKeybord(binding.include.editSearch);
                binding.include.editSearch.setFocusableInTouchMode(true);
                break;
        }
    }
}